package org.darcy.youchat.utils;

import android.content.Context;

import com.netease.nimlib.sdk.media.record.AudioRecorder;
import com.netease.nimlib.sdk.media.record.IAudioRecordCallback;
import com.netease.nimlib.sdk.media.record.RecordType;
import com.netease.nimlib.sdk.msg.constant.SessionTypeEnum;
import com.netease.nimlib.sdk.msg.model.IMMessage;

import org.darcy.youchat.nimsdk.NimMessageSDK;

import java.io.File;

/**
 * 
 * 语音录入工具类
 */
public class AudioRecordService implements IAudioRecordCallback {

    //当前会话类型
    private SessionTypeEnum mSessionType = SessionTypeEnum.P2P;
    //当前会话id
    private String mSessionId;//单聊的联系人的id，群聊是群id

    //录音
    private AudioRecorder mAudioRecorderHelper;
    private boolean mStartRecord;
    private boolean mCanceled;
    private boolean mTouched;

    /**
     * 初始化AudioRecord
     */
    private void initAudioRecord(Context ctx) {
        mStartRecord = true;
        if (mAudioRecorderHelper == null)
            mAudioRecorderHelper = new AudioRecorder(ctx, RecordType.AAC, AudioRecorder.DEFAULT_MAX_AUDIO_RECORD_TIME_SECOND, this);
    }

    /**
     * 开始语音录制
     */
    public void onStartAudioRecord() {
        mAudioRecorderHelper.startRecord();
        mCanceled = false;
        if (!mStartRecord) {
            UIUtils.showToast("初始化录音失败");
            return;
        }
        if (!mTouched) {
            return;
        }
    }

    /**
     * 结束语音录制
     */
    private void onEndAudioRecord(boolean cancel) {
        mAudioRecorderHelper.completeRecord(cancel);
    }

    /**
     * 取消语音录制
     */
    private void cancelAudioRecord(boolean cancel) {
        if (!mStartRecord) {
            return;
        }
        if (mCanceled == cancel) {
            return;
        }
        mCanceled = cancel;
        //updateTimerTip(cancel);
    }

    /**
     * 发送语音消息
     */
    private void sendAudioMsg(File audioFile, long audioLength) {
        IMMessage msg = NimMessageSDK.createAudioMessage(mSessionId, mSessionType, audioFile, audioLength);
        NimMessageSDK.sendMessage(msg);
    }




    @Override
    public void onRecordReady() {

    }

    @Override
    public void onRecordStart(File audioFile, RecordType recordType) {

    }

    @Override
    public void onRecordSuccess(File audioFile, long audioLength, RecordType recordType) {

    }

    @Override
    public void onRecordFail() {

    }

    @Override
    public void onRecordCancel() {

    }

    @Override
    public void onRecordReachedMaxTime(int maxTime) {

    }
}
